<?
function incarca($image,$folder,$cale,$cale_th,$redim,$size,$size_th,$mica){	
	$type = end(explode(".",strtolower($_FILES[$image]['name'])));

	if($type != "jpg" && $type != "jpeg" && $type != "gif" && $type != "png"){
		return array("The accepted picture formats are jpg, png and gif.","","");
	}
	
	move_uploaded_file($_FILES[$image]['tmp_name'],$folder."/".$cale.".".$type);
	
	if($type == "jpg" || $type == "jpeg"){
		$img = imagecreatefromjpeg($folder."/".$cale.".".$type);
	}else if($type == "gif"){
		$img = imagecreatefromgif($folder."/".$cale.".".$type);
	}else if($type == "png"){
		$img = imagecreatefrompng($folder."/".$cale.".".$type);
	}
	
	list($w,$h) = getimagesize($folder."/".$cale.".".$type);
	$procent = $w/$h;
	if($redim == 'width'){
		if($size > $w){
			$nw = $size;
			$nh = $size/$procent;
		}else{
			$nw = $w;
			$nh = $h;
		}
	}else{
		if($size > $h){
			$nh = $size;
			$nw = $size*$procent;
		}else{
			$nw = $w;
			$nh = $h;
		}
	}
	$new_image = imagecreatetruecolor($nw, $nh);
	imagecopyresampled($new_image, $img, 0, 0, 0, 0, $nw, $nh, $w, $h);
	if($type == "jpg" || $type == "jpeg"){
		imagejpeg($new_image,$folder."/".$cale.".".$type,100);
	}else if($type == "gif"){
		imagegif($new_image,$folder."/".$cale.".".$type);
	}else if($type == "png"){
		imagepng($new_image,$folder."/".$cale.".".$type);
	} 
	if($mica){
	
		if($type == "jpg" || $type == "jpeg"){
			$img = imagecreatefromjpeg($folder."/".$cale.".".$type);
		}else if($type == "gif"){
			$img = imagecreatefromgif($folder."/".$cale.".".$type);
		}else if($type == "png"){
			$img = imagecreatefrompng($folder."/".$cale.".".$type);
		}
	
		if($redim == 'width'){
			$nw1 = $size_th;
			$nh1 = $size_th/$procent;
		}else{
			$nh1 = $size_th;
			$nw1 = $size_th*$procent;
		}
		$new_image1 = imagecreatetruecolor($nw1, $nh1);
		imagecopyresampled($new_image1, $img, 0, 0, 0, 0, $nw1, $nh1, $nw, $nh);
		
		if($type == "jpg" || $type == "jpeg"){
			imagejpeg($new_image1,$folder."/".$cale_th.".".$type,100);
		}else if($type == "gif"){
			imagegif($new_image1,$folder."/".$cale_th.".".$type,100);
		}else if($type == "png"){
			imagepng($new_image1,$folder."/".$cale_th.".".$type,100);
		}
	}
	return array("ok",$cale.".".$type,$cale_th.".".$type);
}
?>